import axios from 'axios';
import Toast from 'muse-ui-toast';

export const urlType = 0;
let baseUrl = '/api';
if(urlType){
  baseUrl = 'https://'+window.location.host+'/';
}

axios.defaults.timeout = 5000;
axios.defaults.baseURL =baseUrl; //填写域名


//http request 拦截器
axios.interceptors.request.use(
  config => {
    /*console.log(config)*/
    /*config.data = JSON.stringify(config.data);*/
    config.headers = {
      /*'Content-Type':'application/x-www-form-urlencoded'*/
    }
    return config;
  },
  error => {
    Toast.error('加载超时');
    return Promise.reject(err);
  }
);

//http response响应拦截器即异常处理
axios.interceptors.response.use(response => {
  return response
}, err => {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        Toast.error('错误请求');
        break;
      case 401:
        Toast.error('未授权，请重新登录');
        break;
      case 403:
        Toast.error('拒绝访问');
        break;
      case 404:
        Toast.error('请求错误,未找到该资源');
        break;
      case 405:
        Toast.error('请求方法未允许');
        break;
      case 408:
        Toast.error('请求超时');
        break;
      case 500:
        Toast.error('服务器端出错');
        break;
      case 501:
        Toast.error('网络未实现');
        break;
      case 502:
        Toast.error('网络错误');
        break;
      case 503:
        Toast.error('服务不可用');
        break;
      case 504:
        Toast.error('网络超时');
        break;
      case 505:
        Toast.error('http版本不支持该请求');
        break;
      default:
        Toast.error(`连接错误${err.response.status}`);
    }
  } else {
    Toast.error('连接到服务器失败');
  }
  return Promise.resolve(err.response)
})


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url,params={}){
  return new Promise((resolve,reject) => {
    axios.get(url,{
      params:params
    })
      .then(response => {
        if(response == undefined || response == null || response == ''){
          return;
        }
        if (typeof(response) != 'object') {
          response = JSON.parse(response);
        }
        var str = response.data;
        if((typeof str=='string')&&str.constructor==String){
          Toast.error('请求失败，请稍后再试');
          return;
        }
        var status = response.data.status;
        if(status == 0){
          Toast.error(response.data.msg);
          return;
        }
        resolve(response.data);

      })
      .catch(err => {
        Toast.error('请求失败');
        reject(err)
      })
  })
}


/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url,data = {}){
  $('button').attr('disabled',true);
  return new Promise((resolve,reject) => {
    axios.post(url,data)
      .then(res => {
        var ajaxres = res.data;
        if (ajaxres.msg != '' && ajaxres.msg != undefined) {
          var status = ajaxres.status;
          if(status == 1){
            Toast.success(ajaxres.msg)
          }else {
            Toast.error(ajaxres.msg)

          }
        }
        resolve(ajaxres);
        $('button').attr('disabled',false);
      },err => {
        setTimeout(function () {
          $('button').attr('disabled',false);
        },3000)
        reject(err)
      })
  })
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.patch(url,data)
      .then(response => {
        resolve(response.data);
      },err => {
        reject(err)
      })
  })
}


/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.put(url,data)
      .then(response => {
        resolve(response.data.data);
      },err => {
        reject(err)
      })
  })
}

export const yesS = {
  getCodeImg: baseUrl,
  //编辑或新增地址
  addEditAddress: function(paramObj){
    return post('/address/add',paramObj);
  },
  //请求地址列表
  addressList: function (paramObj) {
    return get('/address/list',paramObj);
  },
  //确认删除地址
  sureDeleteAddress:function (paramObj) {
    return post('/address/delete',paramObj);
  },
  //地址列表中设置默认地址
  setDefaultAddress: function (paramObj) {
    return post('/address/set-default',paramObj);
  },
  //编辑地址时请求地址详情
  getAddressDetail: function (paramObj) {
    return get('/address/edit',paramObj);
  },
  memberWaterbill: function (paramObj) {
    return get('/member-waterbill/index',paramObj);
  },
  //获取默认的一条地址
  getDefaultAddress: function (paramObj) {
    return get('/address/index',paramObj);
  },
  //一键订水 中水票下单
  addWaterBillOrder: function (paramObj) {
    return post('/order/add-bill-order',paramObj);
  },
  //一键订水 水票和商品混合列表
  waterBillGoodsList: function (paramObj) {
    return get('/goods/bill',paramObj);
  },
  //水票和商品混合时的下单
  addOrderWbGoods: function (paramObj) {
    return post('/order/new-add-order',paramObj);
  },
  //提现用户信息
  MemberCommission:function (paramObj){
    return get('/member/member-commission',paramObj)
  },
  //佣金明细列表
  commissionList:function (paramObj) {
    return get('/member/commission-list',paramObj)
  },
  //提现明细列表
  withdrawList:function (paramObj) {
    return get('/member/withdraw-list',paramObj)
  },
  //我的团队列表
  referTeamList:function (paramObj) {
    return get('/member/refer-team',paramObj)
  },
  //分销二维码
  commissionQr:function (paramObj) {
    return get('/member/refer',paramObj)
  },
  //提交提现申请
  ApplySubmit:function (paramObj) {
    return post('/member/apply-commission',paramObj)
  },
  //账户移交 - 获取动态码
  memberCaptcha:function (paramObj) {
    return get('/member/captcha',paramObj)
  },
  //账户中心 - 水票列表
  memberBillList:function (paramObj) {
    return get('/member/bill-list',paramObj)
  },
  //账户中心 - 水票明细列表
  memberBillRecord:function (paramObj) {
    return get('/member/bill-record',paramObj)
  },
  //账户中心 - 水桶 -用户信息
  bucketMember:function (paramObj) {
    return get('/member/info',paramObj)
  },
  //账户中心 - 水桶列表明细
  bucketRecordList:function (paramObj) {
    return get('/member/bucket-record',paramObj)
  },
  //账户中心 - 押金明细列表
  memberDeposit:function (paramObj) {
    return get('/member/deposit',paramObj)
  },
  //共享水票列表
  shareBillList:function (paramObj) {
    return get('/share/bill-info',paramObj)
  },
  //共享水票 - 地址列表
  shareBillAddressList:function (paramObj) {
    return get('/share/address-list',paramObj)
  },
  //获取商家设置的购买数量限制数据
  checkBuyNum:function (paramObj) {
    return get('/member/buy-limit',paramObj);
  },
  //判断是否支付成功
  isPaySuccess:function (paramObj) {
    return get('/waterbill/check-status',paramObj);
  },
  //线上订单处理（微信支付时调用）
  orderPay:function (paramObj) {
    return post('/order/pay',paramObj)
  },
  //订单列表
  getOrderList:function (paramObj) {
    return get('/order/index',paramObj)
  },
  //订单列表 - 获取订单详情
  getOrderDetail:function (paramObj) {
    return get('/order/detail',paramObj)
  },

  //关于我们
  getShopInfo:function (paramObj) {
    return get('/shop/info',paramObj)
  },
  //购买水票 - 水票列表
  waterBillList:function (paramObj) {
    return get('/waterbill/index',paramObj)
  },
  //我的水票列表
  myBillList:function (paramObj) {
    return get('/member/my-bill-list',paramObj)
  },
  //获取公告
  getNotice:function (paramObj) {
    return get('/shop/notice',paramObj)
  },
  //共享二维码
  getShareQrCode:function (paramObj) {
    return get('/share/qr-link',paramObj);
  },
  //商城页的轮播图
  getSliderImg:function (paramObj) {
    return get('/shop/index',paramObj)
  },
  //获取配置的支付方式
  getPayType:function (paramObj) {
    return get('/pay/method',paramObj)
  },
  //获取商铺列表
  getShopList:function (paramObj) {
    return get('/member/shop-list',paramObj)
  },
  //设置默认店铺
  changeShopDefualt:function (paramObj) {
    return post('/member/change-shop',paramObj)
  },
  //快捷开店
  shopSimple:function (paramObj) {
    return post('/simple',paramObj)
  },
  // 获取手机验证码(get)
  getSms:function (paramObj) {
    return get('/sms',paramObj)
  },
  //手机验证 - 个人信息填写
  addInfoMation:function (paramObj) {
    return post('/member/add-information',paramObj)
  },
  //商城 - 商品列表
  getGoodsList:function (paramObj) {
    return get('/goods/category',paramObj)
  },
  //选择商铺
  chooseShop:function (paramObj) {
    return get('/shop/index',paramObj)
  },
  //绑定店铺
  bindShop:function (paramObj) {
    return post('/member/bind-shop',paramObj)
  },
  //提现店铺列表
  commissionShopList:function (paramObj) {
    return get('/shop/commission',paramObj)
  }
}


